﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using CodeStash.Common.DataAccess.Repository;
using CodeStash.Common.DataAccess.EntityFramework;

namespace CodeStash.IntegrationTests
{
    [TestFixture]
    public class CreatedTeamTests
    {
        [Test]
        public void SimpleInsertAndDeleteTest()
        {
            Repository<CreatedTeam> createdTeamRepository = new Repository<CreatedTeam>();
            using (var db = new CodeStashEntities("name=CodeStashEntities"))
            {
                createdTeamRepository.EnrolInUnitOfWork(db);
                Guid uniqueId = Guid.NewGuid();
                createdTeamRepository.Add(new CreatedTeam(1, uniqueId));
                db.SaveChanges();

                IEnumerable<CreatedTeam> teams = createdTeamRepository.FindBy(x => x.TeamId == 1 && x.AspMembershipUserId == uniqueId);
                Assert.AreEqual(1,teams.Count());

                createdTeamRepository.Remove(teams.First());
                db.SaveChanges();

                teams = createdTeamRepository.FindBy(x => x.TeamId == 1 && x.AspMembershipUserId == uniqueId);
                Assert.AreEqual(0, teams.Count());
            }
        }

        [Test]
        public void SimpleUpdateTest()
        {
            Repository<CreatedTeam> createdTeamRepository = new Repository<CreatedTeam>();
            using (var db = new CodeStashEntities("name=CodeStashEntities"))
            {
                createdTeamRepository.EnrolInUnitOfWork(db);
                Guid uniqueId = Guid.NewGuid();
                createdTeamRepository.Add(new CreatedTeam(1, uniqueId));
                db.SaveChanges();

                IEnumerable<CreatedTeam> teams = createdTeamRepository.FindBy(x => x.TeamId == 1 && x.AspMembershipUserId == uniqueId);
                Assert.AreEqual(1, teams.Count());

                Guid uniqueId2 = Guid.NewGuid();
                CreatedTeam addedTeam = teams.First();
                int newTeamId = 2;
                addedTeam.TeamId = newTeamId;
                addedTeam.AspMembershipUserId = uniqueId2;
                db.SaveChanges();

                int id = addedTeam.CreatedTeamId;

                teams = createdTeamRepository.FindBy(x => x.CreatedTeamId == id);
                CreatedTeam updatedTeam = teams.First();
                Assert.AreEqual(1, teams.Count());
                Assert.AreEqual(newTeamId, updatedTeam.TeamId);
                Assert.AreEqual(uniqueId2, updatedTeam.AspMembershipUserId);


                createdTeamRepository.Remove(teams.First());
                db.SaveChanges();

                teams = createdTeamRepository.FindBy(x => x.CreatedTeamId == id);
                Assert.AreEqual(0, teams.Count());
            }
        }

    }
}
